package com.sino.operation.service.business;

import com.alibaba.fastjson.JSON;
import com.sino.operation.Exception.sinoexception.TerminalTaskException;
import com.sino.operation.commons.ConstantValue;
import com.sino.operation.commons.response.CenterResponseEnum;
import com.sino.operation.entity.BizOperateLog;
import com.sino.operation.entity.BizTranslist;
import com.sino.operation.mapper.BizTranslistMapper;
import com.sino.operation.param.out.EscBodyByCounterInquire;
import com.sino.operation.param.out.InquirePublicParam;
import com.sino.operation.param.out.TerminalParam;
import com.sino.operation.util.StringUtil;
import com.sino.operation.util.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.HashMap;
import java.util.Map;

@Service
public class TerminalTaskService {
   @Autowired
   private BizTranslistMapper bizTranslistMapper;

    public boolean  terminaHandle(EscBodyByCounterInquire counterInquire){
        BizOperateLog bizOperateLog = new BizOperateLog();
        HashMap<String, Object> stopFlowParam = new HashMap<>(16);
        InquirePublicParam inquirePublicParam = counterInquire.getInquirePublicParam();
        TerminalParam terminalParam= (TerminalParam) counterInquire.getObject();
        String orgCode = inquirePublicParam.getOrgCode();//机构号
        String flowNo = terminalParam.getFlowNo();//任务号
        String journalNo = terminalParam.getJournalNo();//流水号
        String operReasonType = terminalParam.getOperReasonType();//终止原因
        String terminalType = terminalParam.getTerminalType();//终止类型
        String tellerCode = terminalParam.getTellerCode();//柜员号
        String tellerPost = terminalParam.getTellerPost();//柜员岗位

        BizTranslist bizTranslist = bizTranslistMapper.selectById(journalNo);
        String transState = bizTranslist.getTransState();
        if ("4".equals(transState)){
            return false;
        }
        bizOperateLog.setId(StringUtil.getId());
        bizOperateLog.setJournalNo(journalNo);
        bizOperateLog.setOperReason(terminalType);
        bizOperateLog.setOperReason(operReasonType);
        bizOperateLog.setTellerNo(tellerCode);
        bizOperateLog.setTellerPost(tellerPost);
        bizOperateLog.setOrganId(orgCode);
        bizOperateLog.setOperDate(TimeUtil.getStandardTime());
        stopFlowParam.put("processInstanceId",flowNo);
        RestTemplate restTemplate = new RestTemplate();
        String result = restTemplate.postForObject(ConstantValue.FLOWMANAGER + ConstantValue.STOP_TASK, stopFlowParam, String.class);
        Map map = JSON.parseObject(result, Map.class);
        String code = map.get("code").toString();
        if (!"200".equals(code)){
            return false;
        }
        return true;
    }
}
